var editFlag=false

// 商品清单
function shopping(user_id){
    var p=new Promise(function(resolve,reject){
        $.ajax({
            type: "get",
            url: "../php/shopping.php",
            data: {
                user_id:user_id
            },
            dataType: "json",
            success: function(data) {
                resolve(data)
            }
        })
    })
    return p
}
var total=location.search.split("?")[1].split("&")[0].split("=")[1]
var user_id=location.search.split("?")[1].split("&")[1].split("=")[1]
$(".totalprice2").html(total+".00")
shopping(user_id).then(function(e){
    console.log(e)
    var html=""
    e.forEach(item => {
        html+=`
        <li><div class="img"><img src="${item.goods_pic}" alt=""></div>
            <div class="goodsinfo">
                <p class="title">${item.goods_name}</p>
                <p>数量：<span class="num">${item.goods_num}</span></p>
                <p><span class="price">${item.goods_price}</span></p>
            </div>
        </li>`
    });
    $(".goods_list ul").html(html)
})
// 省市联动
       function a(){
        var province=document.getElementsByClassName("provice")[0]
        var city=document.getElementsByClassName("city")[0]
        var area=document.getElementsByClassName("area")[0]
        var xhr=new XMLHttpRequest()
        xhr.open("get","http://api.yytianqi.com/citylist/id/2")
        xhr.send()
        xhr.onreadystatechange=function(){
        if(xhr.readyState==4&&xhr.status==200){
            var arr=JSON.parse(xhr.responseText)//整个数组
            // console.log(arr)
            var html1=`<option value="">请选择省份</option>`
            arr.list.forEach(item => {
                html1+=`<option>${item.name}</option>`
            });
            province.innerHTML=html1
        }
        province.onchange=function(){
            var html2=`<option value="">请选择城市</option>`
            var html3=`<option value="">请选择地区</option>`
            console.log(this.value)
            if(this.value){
                 var arrcity=(arr.list.filter(item=>this.value==item.name)[0].list)
                //  console.log(arrcity)
                 arrcity.forEach(item => {
                     html2+=`<option value="${item.city_id}">${item.name}</option>`
                 });
            }
            else{
                html2=`<option value="">请选择城市</option>` 
                html3=`<option value="">请选择地区</option>`
            }
            city.innerHTML=html2
            area.innerHTML=html3
        }
        city.onchange=function(){
        //    绑定id
        // if(this.value){
        //     var arrcity=(arr.list.filter(item=>province.value==item.name)[0].list)
        //     arrcity.filter((item)=>{this.value=item.city_id})[0].list
        // } 

        // 不绑定id
        // console.log(this.value,province.value)
        html3=`<option value="">请选择地区</option>`       
            if(this.value){
                var arrcity=(arr.list.filter(item=>province.value==item.name)[0].list)
                console.log(arrcity)
                var arrarea=(arrcity.filter(item=>this.value==item.city_id)[0].list)
                // console.log(arrarea)
                if(arrarea){
                arrarea.forEach(item => {
                    html3+=`<option value="">${item.name}</option>`
                });
                }
                else{
                    html3=`<option value="">请选择地区</option>` 
                }
            }
            else{
                html3=`<option value="">请选择地区</option>`
            }
        area.innerHTML=html3
        }
        }
       }
       
    //  功能
    function editaddress(user_id,user_name,user_phone,user_address,user_id2,user_name2){
        var p=new Promise(function(resolve,reject){
            $.ajax({data:{
                    user_id:user_id,
                    user_name:user_name,
                    user_phone:user_phone,
                    user_address:user_address,
                    user_id2:user_id2,
                    user_name2:user_name2
                },
            type: "get",  
            url:"../php/editaddress.php",
            dataType:"json",
            success:function(e){
                    resolve(e)
            }})
            })
            return p  
    }

    function addCar(user_id,user_name,user_phone,user_address){
        var p=new Promise(function(resolve,reject){
                $.ajax({data:{
                        user_id:user_id,
                        user_name:user_name,
                        user_phone:user_phone,
                        user_address:user_address,
                    },
                type: "get",  
                url:"../php/useraddress.php",
                dataType:"json",
                success:function(e){
                        resolve(e)
                }})
                })
                return p   
    }
// 删除
    function deleteaddress(user_name,user_phone){
        var p=new Promise(function(resolve,reject){
                $.ajax({data:{
                        user_name:user_name,
                        user_phone:user_phone,
                    },
                type: "get",  
                url:"../php/deleteaddress.php",
                dataType:"json",
                success:function(e){
                        resolve(e)
                }})
                })
                return p   
    }
// 获取所有数据
    function address(user_id){
        var p=new Promise(function(resolve,reject){
                $.ajax({data:{
                        user_id:user_id,
                    },
                type: "get",  
                url:"../php/address.php",
                dataType:"json",
                success:function(e){
                        resolve(e)
                }})
                })
                return p   
    }

   var userdata
   function updatepage(){
    address(user_id).then(function(e){
        console.log(e)
        var html=""
        e.forEach(item => {
            var {user_id,user_name,user_phone,user_address}=item
            html+=` <li><input type="checkbox" name="" id="">
            <span class="user_name">${user_name}</span>
            <span>|</span>
            <span class="user_address">${user_address}</span>
            <span>|</span>
            <span class="user_phone">${user_phone}</span>
             <span class="active"></span>
             <span class="delete">删除</span>
             <span class="edit">编辑</span>
             </li>`
        });
        $(".useraddress").html(html)
        userdata=e
        $(".delete").on("click",active)
        $(".edit").on("click",edit)
        $(".useraddress li:first input").prop("checked",true)
        $(".useraddress li:first .active").addClass("mrclass")
        $(".useraddress li:first .active").html("默认地址")
        mraddress()
   })
}
updatepage()

// 删除
function active(){
        user_name=$(this)[0].parentElement.children[1].innerHTML
        user_phone=$(this)[0].parentElement.children[5].innerHTML
        deleteaddress(user_name,user_phone)
        $(this).parent().remove()
    } 
$(".delete").on("click",active)

// 新增
$(".add").click(function(){
    $(".shadow").css({
        display:"block",
    })
    a()
    editFlag=false
    console.log(editFlag)
    // $(".save").on("click",save)
    
})
$(".edit").on("click",edit)
function save(){
    console.log(editFlag)
    if(editFlag==false){
        console.log(111)
        var user_name=$(".name").val()
        var user_phone=$(".phone").val()
        var provice=$(".provice option:selected").text()
        var city=$(".city option:selected").text()
        var area=$(".area option:selected").text()
        var address=$(".adress").val()
        var code=$(".code").val()
        if(provice=="请选择省份"){
             provice=""
        }
        if(city=="请选择城市"){
             city=""
         }
         if(area=="请选择地区"){
             area=""
         }
         user_address=provice+city+area+address
         addCar(user_id,user_name,user_phone,user_address).then(function(e){
             if(e.status){
                updatepage()
             }
         })
        //  $(".edit").on("click",edit)
         cancel()
    }
    else{
        console.log(222)
        var user_name=$(".name").val()
        var user_phone=$(".phone").val()
        var provice=$(".provice option:selected").text()
        var city=$(".city option:selected").text()
        var area=$(".area option:selected").text()
        var address=$(".adress").val()
        var code=$(".code").val()
        if(provice=="请选择省份"){
            provice=""
        }
        if(city=="请选择城市"){
            city=""
        }
        if(area=="请选择地区"){
            area=""
        }
        user_address=provice+city+area+address
        console.log(user_name,user_phone,provice)
        editaddress(user_id,user_name,user_phone,user_address,user_phone2,user_name2).then(function(e){
            console.log(e)
        })
        $(that).parent().find(".user_name").html(user_name)
        $(that).parent().find(".user_phone").html(user_phone)
        $(that).parent().find(".user_address").html(user_address)
        cancel()
    }
}
$(".save").on("click",save)
// 新增save
var that
function edit(){
    that=this
    $(".shadow").css({
        display:"block",
    })
    a()
    editFlag=true
    user_name2=$(that)[0].parentElement.children[1].innerHTML
    user_phone2=$(that)[0].parentElement.children[5].innerHTML
    $(".addinfo .name").val(user_name2)
    $(".addinfo .phone").val(user_phone2)
    // $(".save").on("click",save)
}
// 取消
function cancel(){
    $(".shadow .name").val("")
    $(".shadow .phone").val("")
    $(".shadow .provice option:selected").text("请选择省份")
    $(".shadow .city option:selected").text("请选择城市")
    $(".shadow .area option:selected").text("请选择地区")
    // $(".shadow adress").val("")
    // $(".shadow code").val("")
    $(".adress").val("")
    $(".code").val("")

    $(".shadow").css({
        display:"none",
        })
}
$(".cancel").on("click",cancel)
$(".shadow h3 img").on("click",cancel)

// 默认地址
function mraddress(){
    $("input").on("click",function(){
        if($(this).prop("checked")){
            console.log($(this).parent().find(".active"))
            $(this).parent().find(".active").addClass("mrclass")
            $(this).parent().find(".active").html("默认地址")
            $(this).parent().siblings().find(".active").removeClass("mrclass")
            $(this).parent().siblings().find(".active").html("")
            $(this).parent().siblings().find("input").prop("checked",false)
        }
        else{
            $(this).parent().find(".active").removeClass("mrclass")
            $(this).parent().find(".active").html("")
        }
    })
}


